package com.ttg.demopaidui;
//~--- non-JDK imports --------------------------------------------------------
import java.util.TreeMap;

import com.alipay.api.internal.util.AlipaySignature;
import com.ttg.entitypaidui.PayQrCodeReq;
import com.ttg.utilspaidui.PdPtieData;
import com.ttg.utilspaidui.Constant;
import com.ttg.utilspaidui.TLinx2Util;
import net.sf.json.JSONObject;
//~--- JDK imports ------------------------------------------------------------
//~--- classes ----------------------------------------------------------------
/**
 * Class TradeDemo
 * Description
 * Create 2017-04-19 15:58:31
 *
 * @author Benny.YEE
 */
public class TradeDemo {
  /**
   * Method payQrCode
   */
  private void payQrCode(PayQrCodeReq payQrCodeReq) {
    // 初始化参数   时间
    try {
      // 请求参数的map
      TreeMap<String, String> postmap = new TreeMap<String, String>();
      postmap.put("merchant_code", payQrCodeReq.getMerchantCode());
      postmap.put("out_trade_no", payQrCodeReq.getOutTradeNo());
      // data参数的map
      TreeMap<String, String> datamap = new TreeMap<String, String>();
      datamap.put("pay_way", payQrCodeReq.getPayWay());
      datamap.put("amount", payQrCodeReq.getAmount());
      datamap.put("notify_url", payQrCodeReq.getNotifyUrl());
      datamap.put("body", payQrCodeReq.getBody());
      datamap.put("nonce_str", payQrCodeReq.getNonceStr());
      postmap.putAll(datamap);
      //1.签名
      String merchantRsa = PdPtieData.getInstance().getString("singbon_merchant_rsa");
      String sign = AlipaySignature.rsaSign(postmap, merchantRsa, Constant.ENCOD_UTF8);
      postmap.put("sign", sign);
      System.out.println("返回字符串=0: " + sign);
      //2 请求、响应
      String rspStr = TLinx2Util.handlePostPaiDui(postmap, Constant.PAY_QRCODE);
      System.out.println("返回字符串=1: " + rspStr);
      // 3 验签  有data节点时才验签
      JSONObject respObject = JSONObject.fromObject(rspStr);
      System.out.println("返回字符串=2: " + respObject);
      Object dataStr = respObject.get("sign");
      if (dataStr != null && !rspStr.isEmpty()) {
        // PdPtieData.getInstance("pdmerchant.properties")
        String aesKey = PdPtieData.getInstance().getString("singbon_merchant_aes");
        if (TLinx2Util.verifyPaiDuiSignWithAes(respObject, Constant.ENCOD_UTF8, aesKey, Constant.AESIV)) {
          System.out.println("验签失败成功");
        }
        else {
          System.out.println("=====验签失败=====");
        }
      }
      else {
        System.out.println("=====没有返回data数据=====");
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Method main
   */
  public static void main(String[] args) {
    PayQrCodeReq payQrCodeReq = new PayQrCodeReq();
    payQrCodeReq.setMerchantCode(PdPtieData.getInstance().getString("singbon_merchant_no"));
    payQrCodeReq.setOutTradeNo(String.valueOf(System.currentTimeMillis()));
    payQrCodeReq.setPayWay("2");
    payQrCodeReq.setAmount("1");
    payQrCodeReq.setNotifyUrl("www.singbon.com");
    payQrCodeReq.setBody("180901zchpay");
    payQrCodeReq.setNonceStr(TLinx2Util.getNonceStr());
    new TradeDemo().payQrCode(payQrCodeReq);
  }
}
